﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;

namespace HZ.DrugTrade.Web.Hospitals.StorageManage
{
    public partial class StoragedGoods : BasePage
    {
        public StoragedGoods()
        {
            this.ModuleNo = Module.入库单管理.ToString();
        }
        private int totalRecord;
        private IList<HZ.DrugTrade.Model.View.Distribution> PurchaseOrderDetailList;
        private IList<Model.View.PurchaseOrderDetail> aPurchaseOrderDetailList = new List<Model.View.PurchaseOrderDetail>();

        private IList<Distribution> aDistributionList = new List<Distribution>();
        private IBLL_Purchase iPurchase = Factory.BLL_Purchase();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HZ.DrugTrade.Web.Function.IsThreeLevel())//项目查询条件仅三级医院可见
            {
                this.ddlProjectid.Visible = false;
                this.lblProjectid.Visible = false;
            }

            if (!IsPostBack)
            {
                Binddata();
            }

        }


        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            Binddata();
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            Binddata();
        }

        protected void Binddata()
        {
            Model.Query.PurchaseOrderDetail qryCondition = new Model.Query.PurchaseOrderDetail();
            qryCondition.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            qryCondition.OrderName = txtOrderName.Text.Trim();
            qryCondition.GoodsName = txtGoodsName.Text.Trim();
            //qryCondition.ProjectID = this.ddlProjectid.SelectedValue;
            if (this.ddlProjectid.Visible != false)
            {
                qryCondition.ProjectID = this.ddlProjectid.SelectedValue;
            }
            else
            {
                qryCondition.ProjectID = "0001";
            }
            //qryCondition.PinyinCode = txtPinyinCode.Text.Trim();
            qryCondition.CompanyName_PS = txtCompanyName_PS.Text.Trim();
            qryCondition.PinyinCode = this.txtpinyincode.Text.Trim();
            qryCondition.CompanyName_SC = txtCompanyName_SC.Text.Trim();
            qryCondition.Material = txtMaterial.Text.Trim();
            qryCondition.Outlookc = txtOutLook.Text.Trim();
            qryCondition.SortFields = " PlanTime desc,OrderName ASC ,goodsid";

            aPurchaseOrderDetailList = iPurchase.GetPurchaseOrderDetailInflow_ByWhere(qryCondition, this.AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);


            this.gvwProduceCatalog.AutoGenerateColumns = false;
            gvwProduceCatalog.DataSource = aPurchaseOrderDetailList;
            gvwProduceCatalog.DataBind();
            this.AspNetPager1.RecordCount = totalRecord;

            //隐藏不需要的列
            gvwProduceCatalog.Columns[gvwProduceCatalog.Columns.Count - 1].Visible = true;


        }


        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

            }


        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtOrderName.Text = "";
                //txtPinyinCode.Text = "";
                txtCompanyName_SC.Text = "";
                txtCompanyName_PS.Text = "";
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            Model.Query.PurchaseOrderDetail qryCondition = new Model.Query.PurchaseOrderDetail();
            qryCondition.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            qryCondition.OrderName = txtOrderName.Text.Trim();
            qryCondition.GoodsName = txtGoodsName.Text.Trim();
            //qryCondition.ProjectID = this.ddlProjectid.SelectedValue;
            if (this.ddlProjectid.Visible != false)
            {
                qryCondition.ProjectID = this.ddlProjectid.SelectedValue;
            }
            else
            {
                qryCondition.ProjectID = "0001";
            }
            //qryCondition.PinyinCode = txtPinyinCode.Text.Trim();
            qryCondition.PinyinCode = this.txtpinyincode.Text.Trim();
            qryCondition.CompanyName_PS = txtCompanyName_PS.Text.Trim();
            qryCondition.CompanyName_SC = txtCompanyName_SC.Text.Trim();
            qryCondition.Material = txtMaterial.Text.Trim();
            qryCondition.Outlookc = txtOutLook.Text.Trim();
            qryCondition.SortFields = " PlanTime desc,OrderName ASC ,goodsid";
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.PurchaseOrderDetail>(qryCondition, true);
            string sql = string.Format(@"SELECT Goodsid 产品编号,Goodsname 产品名称,Outlookc 规格型号,Material 材质,Unit 最小计量单位,QualityLevel 产品类别,CompanyName_SC 生产企业,
                                CompanyName_PS 配送企业,OrderName 采购单名称,ProcurePrice 采购限价,Price 采购价,PurchaseCount 采购总量,TotalAmount 采购单总额,
                            DistributeCount 配送总量,DistributeAmount 配送总额,WarehouseCount 入库总量,WarehouseAmount 入库总额 FROM View_PurchaseOrderDetail WHERE 1=1 and WarehouseCount > 0 {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "入库详情.xls");
        }
    }
}
